package com.nuanshui.heatedloan.credit.service.job;

import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import com.nuanshui.heatedloan.task.annotation.Task;
import com.nuanshui.heatedloan.task.facade.bean.JobParams;
import com.nuanshui.heatedloan.task.failover.TaskfailoverStrategyType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wangxw on 2017/7/25.
 */
@Component
public class OrderManagementJob {

    private Logger logger = LoggerFactory.getLogger(OrderManagementJob.class);

    @Autowired
    private RabbitmqMessageSender rabbitmqMessageSender;

    /***
     * 处理贷款到期的定时任务的回调
     * @param jobParams
     */
    @Task(name = "REFUND_EXPIRERE_JOB", group = "REFUND_EXPIRERE", failover= TaskfailoverStrategyType.RETRY,retries=1)
    public void dealExpireRefundJob(JobParams jobParams){
        LogUtil.info(logger, "---------------开始发送执行贷款到期任务------------------");
        String orderNum = (String) jobParams.get(JobParams.ORDER_NUM);
        LogUtil.info(logger, String.format("---orderNum : %s ---------------------------------------", orderNum));
        rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_REFUND_EXPIRE_JOB, orderNum, true);
        LogUtil.info(logger, "----------------完成发送执行贷款到期任务------------------");
    }

    /***
     * 处理贷款提醒的定时任务
     * @param jobParams
     */
    @Task(name = "REMINDER_REFUND_JOB", group = "REMINDER_REFUND", failover= TaskfailoverStrategyType.RETRY,retries=1)
    public void dealReminderRefundJob(JobParams jobParams){
        LogUtil.info(logger, "---------------开始发送执行贷款提醒的任务------------------");
        String orderNum = (String) jobParams.get(JobParams.ORDER_NUM);
        LogUtil.info(logger, String.format("---orderNum : %s ---------------------------------------", orderNum));
        rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_REMINDER_REFUND_JOB, orderNum, true);
        LogUtil.info(logger, "----------------完成发送执行贷款提醒的任务------------------");
    }

    /***
     * 处理贷款逾期提醒的定时任务
     * @param jobParams
     */
    @Task(name = "LATE_REMIND_JOB", group = "LATE_REMIND", failover= TaskfailoverStrategyType.RETRY,retries=1)
    public void dealLateRemindJob(JobParams jobParams){
        LogUtil.info(logger, "---------------开始发送执行逾期提醒的任务------------------");
        String orderNum = (String) jobParams.get(JobParams.ORDER_NUM);
        LogUtil.info(logger, String.format("---orderNum : %s ---------------------------------------", orderNum));
        rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_LATE_REMIND_JOB, orderNum, true);
        LogUtil.info(logger, "---------------完成发送执行逾期提醒的任务------------------");
    }

    /***
     * 处理更新逾期金额和逾期时间的定时任务
     * @param jobParams
     */
    @Task(name = "CALCULATE_LATE_PAYMENT_JOB", group = "CALCULATE_LATE_PAYMENT", failover= TaskfailoverStrategyType.RETRY,retries=1)
    public void dealCalculateLatepaymentJob(JobParams jobParams){
        LogUtil.info(logger, "---------------开始发送执行逾期费用计算任务------------------");
        String orderNum = (String) jobParams.get(JobParams.ORDER_NUM);
        LogUtil.info(logger, String.format("---orderNum : %s ---------------------------------------", orderNum));
        rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_CALCULATE_LATE_PAYMENT_JOB, orderNum, true);
        LogUtil.info(logger, "---------------完成发送执行逾期费用计算任务-----------------");
    }


    public static void main(String[] args) throws Exception{
        getStrTime(1502121600000l);
    }

    public static void getStrTime(long cc_time) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      // 例如：cc_time=1291778220
        String d = sdf.format(cc_time);
        Date date=sdf.parse(d);
        System.out.println("Format To String(Date):"+d);
        System.out.println("Format To Date:"+date);
    }

}
